package com.weisys.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@RestController
public class ConsumerController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate template;

    @GetMapping("/instances")
    public List<ServiceInstance> instances() {
        List<ServiceInstance> providers =  this.discoveryClient.getInstances("provider");
        return  providers;
    }

    @GetMapping("/index")
    public String index() {
         return "调用服务：" + this.template.getForObject("http://provider/index", String.class);
    }

    @GetMapping("/list")
    public String list() {
        return "调用服务：" + this.template.getForObject("http://provider/list", String.class);
    }

    @GetMapping("/hot")
    public String hot(@RequestParam(value="p1", required = false) int p1, @RequestParam(value = "p2", required = false) int p2) {
        return "调用服务：" + this.template.getForObject("http://provider/hot?p1="+p1+"&p2="+p2, String.class);
    }
}
